/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cip.esymons.colegiatura.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import org.cip.esymons.colegiatura.beans.areaController;
import org.cip.esymons.general.be.Area;


/**
 *
 * @author ADMINISTRADOR
 */
public class areaConverter implements Converter {

    public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) throws ConverterException {

        if (arg2 == null) {
            return null;
        }
        Integer codigo=null;
        try {
            codigo = new Integer(arg2);
        } catch (Exception ex) {
            return null;
        }
        areaController controller = (areaController) FacesContext.getCurrentInstance().getApplication().getVariableResolver().resolveVariable(FacesContext.getCurrentInstance(), "areaController");
        return controller.buscarArea(codigo);
    }

    public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) throws ConverterException {

        if (arg2 == null) {
            return null;
        }
        if (arg2 instanceof Area) {
            if (((Area) arg2).getAreCodigo() == null) {
                return null;
            } else {
                return ((Area) arg2).getAreCodigo().toString();
            }
        } else {
            return null;
        }
    }
    
}
